package com.yuntsg.ruiijn.controller.search;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SimpleQuery;
import com.yuntsg.ruiijn.databse_search.entity.Customization;
import com.yuntsg.ruiijn.databse_search.entity.CustomizationDetail;
import com.yuntsg.ruiijn.databse_search.service.CustomizationDetailService;
import com.yuntsg.ruiijn.databse_search.service.CustomizationService;
import com.yuntsg.ruiijn.databse_search.service.SearchSelfService;
import common.bean.R;
import common.bean.RRException;
import common.bean.SelectedData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("manager/pub/alternative")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@SuppressWarnings("all")
@Slf4j
public class AlternativeController {


    private final CustomizationDetailService customizationDetailService;

    private final CustomizationService customizationService;

    private final SearchSelfService searchSelfService;


    /**
     * 备选列表
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("records")
    @ResponseBody
    public R records(@RequestParam("id") Integer id) {
        List<Integer> ids = SimpleQuery.list(Wrappers.<Customization>lambdaQuery().eq(Customization::getRid, id), Customization::getId);
        if (null == ids || ids.size() == 0) {
            return R.ok().put("list", null);
        }
        return R.ok().put("list", customizationService.listByIds(ids));
    }

    /**
     * 添加到备选
     *
     * @param content
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public R add(@RequestBody SelectedData[] data) throws IOException {
        if (null != data && data.length > 0) {
            if (customizationDetailService.count(Wrappers.<CustomizationDetail>lambdaQuery().eq(CustomizationDetail::getRid, data[0].getRid()).eq(CustomizationDetail::getOriginal, data[0].getOriginal())) > 0) {
                throw new RRException("已存在,请勿重复添加");
            }
            Customization customization = new Customization();
            customization.setRid(data[0].getRid());
            customization.setOriginal(data[0].getOriginal());
            customization.setStatus(0);
            customization.setCreateTime(System.currentTimeMillis());
            customizationService.saveOrUpdate(customization);
            for (SelectedData selectedData : data) {
                CustomizationDetail selected = new CustomizationDetail();
                selected.setRid(selectedData.getRid());
                selected.setOriginal(selectedData.getOriginal());
                selected.setSimilarArticles(selectedData.getSimilarArticles());
                selected.setSimilarity(selectedData.getSimilarity());
                selected.setFid(customization.getId());
                selected.setStatus(0);
                selected.setCreateTime(System.currentTimeMillis());
                customizationDetailService.saveOrUpdate(selected);
            }
        }
        return R.ok();
    }

    /**
     * 删除备选
     *
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    @ResponseBody
    public R delete(@PathVariable("id") Integer id) {
        customizationService.removeById(id);
        customizationDetailService.removeByIds(SimpleQuery.list(Wrappers.<CustomizationDetail>lambdaQuery().eq(CustomizationDetail::getFid, id), CustomizationDetail::getId));
        return R.ok();
    }


}